package gui.basicchart;

import javax.swing.JPanel;

import model.Node;
import model.listener.NodeListener;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class BasicChart extends JPanel implements NodeListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 977616851597306802L;
	private XYSeries[] series;
	private XYSeriesCollection dataset = new XYSeriesCollection();
	private int maxUsers;
	private JFreeChart chart;
	private ChartPanel chartPanel;

	public BasicChart(int nbrUsers) {
		super();
		maxUsers = nbrUsers;
		// Create series for each user, each coordinates and each nodes
		series = new XYSeries[maxUsers];

		for (int k = 0; k < maxUsers; k++) {
			series[k] = new XYSeries("User " + k);
			dataset.addSeries(series[k]);
		}
		
		chart = ChartFactory.createXYLineChart("Gravity center variation", "Time (ms)", "position",
				dataset, PlotOrientation.VERTICAL, true, true, true);

		chartPanel = new ChartPanel(chart);
		this.add(chartPanel);
	}

	@Override
	public void data(Node node, float x, float y, float z, int userId, int time) {

		// Check if user is inside the limit of tracking users
		if (userId < maxUsers && node.equals(Node.XN_SKEL_HEAD)) {
			series[userId].add(time, y);
		}
	}

}
